package com.newshub.monitor.controller;

import com.newshub.common.Result;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import com.newshub.monitor.service.PerformanceAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 性能分析控制器
 * 整合慢查询分析和性能报表功能
 */
@Slf4j
@RestController
@RequestMapping("/api/monitor/performance")
@CrossOrigin(origins = "*")
public class PerformanceAnalysisController {
    
    @Autowired
    private PerformanceAnalysisService performanceAnalysisService;

    /**
     * 获取慢查询列表
     */
    @GetMapping("/slow-queries")
    @BusinessLog(module = "性能分析", description = "获取慢查询列表", level = LogLevel.INFO)
    public Result<Map<String, Object>> getSlowQueries(
            @RequestParam(required = false) String sqlType,
            @RequestParam(required = false) String tableName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        try {
            Map<String, Object> result = performanceAnalysisService.getSlowQueries(sqlType, tableName, startTime, endTime, pageNum, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取慢查询列表失败", e);
            return Result.error("获取慢查询列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取慢查询统计
     */
    @GetMapping("/slow-query-stats")
    @BusinessLog(module = "性能分析", description = "获取慢查询统计", level = LogLevel.INFO)
    public Result<Map<String, Object>> getSlowQueryStats(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        try {
            Map<String, Object> result = performanceAnalysisService.getSlowQueryStats(startTime, endTime);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取慢查询统计失败", e);
            return Result.error("获取慢查询统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取慢查询趋势
     */
    @GetMapping("/slow-query-trend")
    @BusinessLog(module = "性能分析", description = "获取慢查询趋势", level = LogLevel.INFO)
    public Result<Map<String, Object>> getSlowQueryTrend(
            @RequestParam(required = false) String sqlType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(defaultValue = "1h") String interval) {
        
        try {
            Map<String, Object> result = performanceAnalysisService.getSlowQueryTrend(sqlType, startTime, endTime, interval);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取慢查询趋势失败", e);
            return Result.error("获取慢查询趋势失败: " + e.getMessage());
        }
    }

    /**
     * 获取性能报表
     */
    @GetMapping("/report")
    @BusinessLog(module = "性能分析", description = "获取性能报表", level = LogLevel.INFO)
    public Result<Map<String, Object>> getPerformanceReport(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(defaultValue = "daily") String reportType) {
        
        try {
            Map<String, Object> result = performanceAnalysisService.getPerformanceReport(startTime, endTime, reportType);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取性能报表失败", e);
            return Result.error("获取性能报表失败: " + e.getMessage());
        }
    }

    /**
     * 获取性能优化建议
     */
    @GetMapping("/optimization-suggestions")
    @BusinessLog(module = "性能分析", description = "获取性能优化建议", level = LogLevel.INFO)
    public Result<Map<String, Object>> getOptimizationSuggestions() {
        try {
            Map<String, Object> result = performanceAnalysisService.getOptimizationSuggestions();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取性能优化建议失败", e);
            return Result.error("获取性能优化建议失败: " + e.getMessage());
        }
    }

    /**
     * 获取数据库性能指标
     */
    @GetMapping("/database-metrics")
    @BusinessLog(module = "性能分析", description = "获取数据库性能指标", level = LogLevel.INFO)
    public Result<Map<String, Object>> getDatabaseMetrics() {
        try {
            Map<String, Object> result = performanceAnalysisService.getDatabaseMetrics();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取数据库性能指标失败", e);
            return Result.error("获取数据库性能指标失败: " + e.getMessage());
        }
    }
}
